#encoding:utf8
#mongo查询
import pymongo

class MongoClient(object) :
    def __init__(self, host, db, port = 27001) :
        self.host = host
        self.port = int(port)
        self.db = db

        self.conn = pymongo.MongoClient(self.host, self.port)
        self.database = self.conn[self.db]

    def execute(self, table, query = "", mode = "select") :
        if query == "" : return None
        try : 
            if mode == "selectone" : return self.database[table].find_one(query)
            if mode == "select" : return self.database[table].find(query)
            if mode == "insert" : return self.database[table].insert(query)
            if mode == "delete" : return self.database[table].remove(query)
            if mode == "update" : return self.database[table].update(query)
            if mode == "index" : return self.database[table].create_index(query)
        except Exception, e :
            print ">>> Mongodb Error [%s]: %s" % (mode,e)
            return None

    def close(self) :
        self.conn.close()

